package com.blockmeta.bbs.immodule.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseImResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.activity.chat.UCGChatActivity;
import com.blockmeta.bbs.immodule.adapter.CircleListAdapter;
import com.blockmeta.bbs.immodule.bean.Params;
import com.blockmeta.bbs.immodule.bean.beaninterface.ICircleEntry;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.corebean.Impl.CircleDomainService;
import com.blockmeta.bbs.immodule.net.api.GetChatGroupByUidApi;
import com.blockmeta.bbs.immodule.net.pojo.IMCircleEntityBook;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author airthor
 * @date 2019/5/15
 */
public class CircleListActivity extends BaseActivity {
    @BindView(R2.id.rcv_circle_list)
    RecyclerView mRcvCircleList;

    private List<ICircleEntry> mICircleEntries = new ArrayList<>();
    private CircleListAdapter mAdapter;
    private Long mUserId;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        return getLayoutInflater().inflate(R.layout.activity_im_circlelist,null);
    }

    @Override
    protected void initialize() {
        super.initialize();
        mUserId = getIntent().getLongExtra(Params.USER_TAG, 0);

        setTitleText("他的圈子");
        initView();
        network();
    }

    private void network() {
        RetrofitFactory.getRetrofit().create(GetChatGroupByUidApi.class)
                .getChatGroupByUid(mUserId)
                .compose(this.<BaseImResponse<List<IMCircleEntityBook>>>bindToLifecycle())
                .compose(RxUtil.<List<IMCircleEntityBook>>commonResult())
                .compose(RxUtil.<List<IMCircleEntityBook>>rxSchedulerHelper())
                .subscribe(new CommonSubscriber<List<IMCircleEntityBook>>(mContext) {
                    @Override
                    public void onNext(List<IMCircleEntityBook> list) {
                        mICircleEntries.addAll(list);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView() {
        mRcvCircleList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CircleListAdapter(R.layout.item_join_circle_list_hot, mICircleEntries);
        mAdapter.bindToRecyclerView(mRcvCircleList);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() != R.id.btn_join) {
                    return;
                }
                joinCircle(mICircleEntries.get(position).getId(), mICircleEntries.get(position).getCircleAuthRule()
                        , mICircleEntries.get(position).getUserLevel(), position);
            }
        });
        //条目点击
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                boolean joinState = mICircleEntries.get(position).getJoinState();
                long circleId = mICircleEntries.get(position).getId();
                //根据加入状态跳转不同界面。已加入的跳转聊天页面；未加入的跳转圈子详情页面。
                if (joinState) {
                    Intent intent = new Intent(mContext, UCGChatActivity.class);
                    intent.putExtra(Params.USER_TAG, circleId);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(mContext, CircleDetailActivity.class);
                    intent.putExtra(Params.CIRCLE_ID, circleId);
                    intent.putExtra(Params.JOIN_STATE, false);
                    startActivity(intent);
                }
            }
        });
    }
    /**
     * 加入推荐圈子
     *  @param id 圈子ID
     * @param circleAuthRule
     * @param position
     */
    private void joinCircle(final long id, int circleAuthRule, int userLevel, final int position) {

        IMApplicationContext.getCircleDomainService().joinCircle(mContext, id, circleAuthRule, userLevel, new CircleDomainService.CircleJoinCallBack(){

            @Override
            public void onSuccess(String message) {
                ToastUtil.show(message);
                //本地更新加入状态
                ICircleEntry item = mAdapter.getItem(position);
                if (item != null) {
                    item.setJoinState(true);
                }
                mAdapter.notifyItemChanged(position);
                RouterHelper.startActivity(mContext
                        , RouterPath.IM_UCGChatActivity_ACTIVITY,String.valueOf(id));
            }

            @Override
            public void onFail() {

            }
        });
    }

}
